package cn.com.haopy.dataes.db.mybatis.plus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

    @Bean
    public MetaObjectHandler metaObjectHandler() {

        String userName = null;

        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                boolean hasGet = metaObject.hasGetter("createTime");
                if (hasGet) {
                    // 强制做一次置空处理，保证正常填充
                    metaObject.setValue("createTime", null);
                    this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
                }

                hasGet = metaObject.hasGetter("createBy");
                if (hasGet) {
                    // 强制做一次置空处理，保证更新时间正常填充
                    metaObject.setValue("createBy", null);
                    this.strictInsertFill(metaObject, "createBy", String.class, userName);
                }

                hasGet = metaObject.hasGetter("updateTime");
                if (hasGet) {
                    // 强制做一次置空处理，保证正常填充
                    metaObject.setValue("updateTime", null);
                    this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
                }
                hasGet = metaObject.hasGetter("updateBy");
                if (hasGet) {
                    // 强制做一次置空处理，保证更新时间正常填充
                    metaObject.setValue("updateBy", null);
                    this.strictInsertFill(metaObject, "updateBy", String.class, userName);
                }

                hasGet = metaObject.hasGetter("deleted");
                if (hasGet) {
                    // 强制做一次置空处理，保证正常填充
                    metaObject.setValue("deleted", null);
                    this.strictInsertFill(metaObject, "deleted", Boolean.class, false);
                }
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                boolean hasGet = metaObject.hasGetter("updateTime");
                if (hasGet) {
                    // 强制做一次置空处理，保证更新时间正常填充
                    metaObject.setValue("updateTime", null);
                    this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
                }

                hasGet = metaObject.hasGetter("updateBy");
                if (hasGet) {
                    // 强制做一次置空处理，保证更新时间正常填充
                    metaObject.setValue("updateBy", null);
                    this.strictUpdateFill(metaObject, "updateBy", String.class, userName);
                }
            }
        };
    }
}
